package com.andy.config;

import com.andy.io.Resources;
import com.andy.pojo.Configuration;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.beans.PropertyVetoException;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;

public class XmlConfigBuilder {

    private Configuration configuration;

    public XmlConfigBuilder(Configuration configuration) {
        this.configuration = configuration;
    }

    /**
     * 解析输入流到configuration
     * @param inputStream
     * @return
     * @throws DocumentException
     * @throws PropertyVetoException
     * @throws ClassNotFoundException
     */
    public Configuration parseConfigration(InputStream inputStream) throws DocumentException, PropertyVetoException, ClassNotFoundException {
        //1-读取输入流
        Document document = new SAXReader().read(inputStream);

        //2-放入dataSource
        setDataSource(document);

        //3-放入MappedStatement
        setMappedStatement(document);

        return configuration;
    }

    /**
     * 放入dateSource
     * @param document
     * @throws PropertyVetoException
     */
    private void setDataSource(Document document) throws PropertyVetoException {
        //存放到properties中
        Properties properties = new Properties();
        Element rootElement = document.getRootElement();
        List<Element> prepertyElementList = rootElement.selectNodes("//property");
        for (Element prepertyElement : prepertyElementList) {
            String name = prepertyElement.attributeValue("name");
            String value = prepertyElement.attributeValue("value");
            //放入单个配置属性
            properties.setProperty(name,value);
        }

        //创建dataSource
        ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
        comboPooledDataSource.setJdbcUrl(properties.getProperty("jdbcUrl"));
        comboPooledDataSource.setDriverClass(properties.getProperty("driverClass"));
        comboPooledDataSource.setUser(properties.getProperty("user"));
        comboPooledDataSource.setPassword(properties.getProperty("password"));

        //放入configuration中
        configuration.setDataSource(comboPooledDataSource);
    }

    /**
     * 放入sql语句的map
     * @param document
     */
    private void setMappedStatement(Document document) throws ClassNotFoundException, DocumentException {
        //sqlMapper.xml的解析器
        XmlMapperBuilder xmlMapperBuilder = new XmlMapperBuilder(configuration);

        //循环所有待解析的xml
        Element rootElement = document.getRootElement();
        List<Element> mapperElementList = rootElement.selectNodes("//mapper");
        for (Element element : mapperElementList) {
            //拿到mapper.xml文件的地址
            String resource = element.attributeValue("resource");
            //mapper.xml的输入流
            InputStream sqlMapResource = Resources.getResourceAsStream(resource);
            //解析mapper.xml
            xmlMapperBuilder.parse(sqlMapResource);
        }
        
    }

}
